package com.knife.blog.logic;

import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;

import com.knife.web.Globals;

public class VelocityFactory {
	private static final Logger logger = (Logger) Logger
			.getLogger(VelocityFactory.class);

	private static VelocityEngine ve;

	private VelocityFactory() {
	}

	private static VelocityEngine getVelocityEngine() {
		if (ve == null)
			doInit();
		return ve;
	}

	private static void doInit() {
		Properties p = new Properties();
		ve = new VelocityEngine();
		// System.out.println(ve==null);
		// System.out.println(Globals.APP_BASE_DIR+BaseSiteConfig.getInstance().getTemplatePath());
		p.setProperty("file.resource.loader.path", Globals.APP_BASE_DIR
				+ Globals.DEFAULT_TEMPLATE_PATH);
		try {
			ve.init(p);
		} catch (Exception e) {
			System.out.println("初始化错误：" + e);
		}
	}

	public static Template getTemplate(String fileName) {
		return getTemplate(fileName, "utf-8");
	}

	public static Template getTemplate(String fileName, String encoding) {
		Template template = null;
		try {
			template = getVelocityEngine().getTemplate(fileName, encoding);
		} catch (Exception e) {
			logger.error(e.getClass().toString() + e);
		}
		return template;
	}
}
